package com.lw.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.lw.entity.TbInterface;
import com.lw.entity.TbUser;
import com.lw.entity.TbUserInterface;
import com.lw.entity.table.TbAlarmTableDef;
import com.lw.entity.table.TbInterfaceTableDef;
import com.lw.entity.table.TbUserInterfaceTableDef;
import com.lw.entity.table.TbUserTableDef;
import com.lw.service.*;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

import static com.lw.entity.table.TbUserInterfaceTableDef.TB_USER_INTERFACE;

@Service
public class IndexServiceImpl implements IndexService {

    @Resource
    TbUserService tbUserService;

    @Resource
    TbAlarmService tbAlarmService;

    @Resource
    TbInterfaceService tbInterfaceService;

    @Resource
    TbUserInterfaceService tbUserInterfaceService;


    @Override
    public JSONObject statIndex(String location) {
        if (StringUtils.hasLength(location)) {
            location = location.equals("江苏省") ? null : location;
        }
        JSONObject result = new JSONObject();
        QueryWrapper queryWrapperUserCount = new QueryWrapper();
        if (StringUtils.hasLength(location)) {
            queryWrapperUserCount.eq("address", location);
        }
        long countUser = tbUserService.count(queryWrapperUserCount);
        //用户统计数
        result.put("countUser", countUser);
        if (countUser == 0) {
            result.put("countInterface", 0);
            result.put("listInterface", new ArrayList<>());
            result.put("totalServiceCount", 0);
            result.put("countAlarmL1", 0);
            result.put("countAlarmL3", 0);
            return result;
        }

        //统计告警数
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(TbAlarmTableDef.TB_ALARM.LEVEL.getName(), 1);
        long countAlarmL1 = tbAlarmService.count(queryWrapper);
        queryWrapper.eq(TbAlarmTableDef.TB_ALARM.LEVEL.getName(), 3);
        long countAlarmL3 = tbAlarmService.count(queryWrapper);

        //告警统计数
        result.put("countAlarmL1", countAlarmL1);
        result.put("countAlarmL3", countAlarmL3);

        //服务用户列表
        QueryWrapper queryWrapperUserList = new QueryWrapper();
        if (StringUtils.hasLength(location)) {
            queryWrapperUserList.eq("address", location);
        }
        List<TbUser> list = tbUserService.list(queryWrapperUserList);
        result.put("userList", list);

        //接口统计数
        List<Integer> ids = new ArrayList<>();
        for (TbUser tbUser : list) {
            ids.add(tbUser.getId());
        }
        QueryWrapper queryWrapperInterfaceCount = new QueryWrapper();
        queryWrapperInterfaceCount.in(TB_USER_INTERFACE.USER_ID.getName(), ids);
        List<TbUserInterface> countInterface = tbUserInterfaceService.list(queryWrapperInterfaceCount);
        //接口统计数
        result.put("countInterface", countInterface.size());
        //接口列表
        List<Integer> interfaceIds = new ArrayList<>();
        for (TbUserInterface tbUserInterface : countInterface) {
            interfaceIds.add(tbUserInterface.getInterfaceId());
        }
        if (interfaceIds.size() == 0) {
            result.put("listInterface", new ArrayList<>());
            result.put("totalServiceCount", 0);
            return result;
        }
        QueryWrapper queryWrapperInterfaceList = new QueryWrapper();
        queryWrapperInterfaceList.in(TbInterfaceTableDef.TB_INTERFACE.ID.getName(), interfaceIds);
        List<TbInterface> listInterface = tbInterfaceService.list(queryWrapperInterfaceList);
        result.put("listInterface", listInterface);
        //统计接口调用次数
        long totalServiceCount = 0;
        for (TbInterface tbInterface : listInterface) {
            if (tbInterface.getServiceCount() != null) {
                totalServiceCount += tbInterface.getServiceCount();
            }
        }
        result.put("totalServiceCount", totalServiceCount);

        return result;
    }
}
